package com.sysware.designpattern.prototype;

import java.util.Arrays;

import org.junit.Test;

public class App {

	@Test
	public void main() {

		Sheep s1 = new Sheep("mmm");
		System.out.println(s1.getName());

		Sheep s2 = s1.clone();
		// 通过s1的原型克隆出s2,但两个不是同一个对象
		boolean isOneObj = (s1 == s2);
		System.out.println(isOneObj);
		// if(!isOneObj) return;
		System.out.println(s2.getName());

		s2.setName("KKK");
		//
		System.out.println(s1.getName() + "--" + s2.getName());

		System.out.println(s1 == s2);
	}

	@Test
	public void arrayCopyTest() {

		String[] sheeps_1 = new String[] { "q1", "q2", "q3", "q4" };

		System.out.println("sheeps_1 " + Arrays.toString(sheeps_1));

		String[] sheeps_2 = null;
		// 1、生成不同对象 且 必须先生成对象
		// sheeps_2 = new String[sheeps_1.length];
		// System.arraycopy(sheeps_1, 0, sheeps_2, 0, sheeps_1.length);

		// 2、底层使用
		sheeps_2 = Arrays.copyOf(sheeps_1, sheeps_1.length);

		sheeps_1[1] = "qw";
		System.out.println("sheeps_1 " + Arrays.toString(sheeps_1));
		System.out.println("sheeps_2 " + Arrays.toString(sheeps_2));
	}

}

class Sheep implements Cloneable {
	private String name;

	public Sheep(String name) {
		this.name = name;
	}

	private Head head;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Head getHead() {
		return head;
	}

	public void setHead(Head head) {
		this.head = head;
	}

	protected Sheep clone() {
		try {
			Object obj = super.clone();
			if (obj instanceof Sheep)
				return (Sheep) obj;
		} catch (CloneNotSupportedException e) {
		}
		return null;
	}

}

class Head {

	private int eye = 2;

	private String f = "666";

	public int getEye() {
		return eye;
	}

	public void setEye(int eye) {
		this.eye = eye;
	}

	public String getF() {
		return f;
	}

	public void setF(String f) {
		this.f = f;
	}

}
